class OrdersController < ApplicationController
  before_action :set_order, only: [:show, :edit, :update, :destroy]
  before_action :set_page_title
  
  # GET /orders
  # GET /orders.json
  def index    
    # if params[:commit].present?
      # logger.info "search clicked ................"
      # logger.info order_params[:customer_id]
      # logger.info build_search
        # @orders = Order.where(build_search).page(params[:page])
#         
    # else
        # # @orders = Order.paginate(:page => params[:page], :per_page => 5)
        # @orders = Order.page(params[:page])
    # end
       
    # build_search
    # @order = Order.new
    @orders = Order.where(build_search(search_params)).order(created_at: :desc).page(params[:page])

    respond_to do |format|
      format.html{}
      format.js {render :layout => false}
      format.json {render json:@orders }
    end
    
  end

  # GET /orders/1
  # GET /orders/1.json
  def show
  end

  # GET /orders/new
  def new
    @order = Order.new
    @order.set_order_code
    @order.set_order_date    
    
    5.times do |order|
      @order.items.build  
    end
    
  end

  # GET /orders/1/edit
  def edit
  end

  # POST /orders
  # POST /orders.json
  def create
    @order = Order.new(order_params)

    respond_to do |format|
      if @order.save
        format.html { redirect_to @order, notice: 'Order was successfully created.' }
        format.json { render :show, status: :created, location: @order }
      else
        format.html { render :new }
        format.json { render json: @order.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /orders/1
  # PATCH/PUT /orders/1.json
  def update
    respond_to do |format|
      if @order.update(order_params)
        format.html { redirect_to @order, notice: 'Order was successfully updated.' }
        format.json { render :show, status: :ok, location: @order }
      else
        format.html { render :edit }
        format.json { render json: @order.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /orders/1
  # DELETE /orders/1.json
  def destroy
    
    @order.destroy
    
    respond_to do |format|
      format.html { redirect_to orders_url, notice: 'Order was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  # PRINT invoice /orders/1/invoice
  def invoice
    
  end
  
  
  def list
    @orders = Order.where(build_search(search_params)).order(created_at: :desc).page(params[:page])

    respond_to do |format|
      format.html{}
      format.js {}
    end
  end


  private
    # Use callbacks to share common setup or constraints between actions.
    def set_order
      @order = Order.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def order_params
      params.require(:order).permit(:order_code, :order_type,:customer_po, :order_date, :amount, :due_date, :payment_term,:contract_no, :tieta_settlement_no,:customer_id, :comment,:shipping_address,:receiver,:receiver_tel, 
                                    order_items_attributes: [:product_pn,:product_name,:product_desc,:unit,:tax,:unit_price,:quantity,:amount,:comment])
                                    
    end
    
    def set_page_title
      @page_title = "订单管理" 
    end
    
    def search_params     
        if params[:order].present?  
          order_params.permit(:order_code,:order_type,:customer_id,:customer_po, :contract_no, :tieta_settlement_no)
        end                     
    end
    
    # def build_search(search_params)
        # sql = ""    
        # if search_params.present?
          # search_params.each do |p|                  
          # if sql.blank? 
            # if p[1].present?
              # sql = sql + "\""+p[0]+"\"='"+p[1]+"'"
            # end
          # else
            # if p[1].present?
              # sql = sql + " AND \""+p[0]+"\"='"+p[1]+"'"
            # end
          # end          
        # end  
        # end       
#                     
        # return sql                              
    # end
    
end
